package com.ruoyi.project.jxy.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.jxy.domain.Corpus;
import com.ruoyi.project.jxy.entity.SkillPara;
import com.ruoyi.project.jxy.mapper.CorpusMapper;
import com.ruoyi.project.jxy.mapper.IntentionMapper;
import com.ruoyi.project.jxy.mapper.SlotEntityRefMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.project.jxy.mapper.SkillMapper;
import com.ruoyi.project.jxy.domain.Skill;
import com.ruoyi.project.jxy.service.ISkillService;

import javax.annotation.Resource;

/**
 * 技能Service业务层处理
 *
 * @author ruoyi
 * @date 2019-12-06
 */
@Service
public class SkillServiceImpl extends ServiceImpl<SkillMapper, Skill> implements ISkillService {
    @Resource
    private IntentionMapper intentionMapper;
    @Resource
    private CorpusMapper corpusMapper;
    @Resource
    private SlotEntityRefMapper slotEntityRefMapper;

    public Map getSkillInfo(SkillPara skill) {

        Map tempMap = new HashMap();
        //返回值
        Map resultMap = new HashMap();
        List resultIntents = new ArrayList();
        //获取技能对应的意图
        List<Map> intentions = intentionMapper.getIntentionInfoBySkillId(skill.getId());
        QueryWrapper wrapper = new QueryWrapper();
        if (intentions != null && intentions.size() > 0) {
            for (Map map :
                    intentions) {
                //返回值中每一个意图
                Map resultIntent = new HashMap();
                //意图id
                Long intentionId = (Long) map.get("intentionId");
                //技能名称
                String skillName = (String) map.get("skillName");
                //技能code
                String skillCode = (String) map.get("skillCode");
                //意图名称
                String intentionName = (String) map.get("intentionName");
                //意图code
                String intentionCode = (String) map.get("intentionCode");
                resultIntent.put("intentcode", intentionCode);
                resultMap.put("skillcode", skillCode);
                resultMap.put("name", skillName);

                //获取每个意图对应的语料
                wrapper = new QueryWrapper();
                wrapper.eq("intention_id", intentionId);
                wrapper.select("solts");
                //语料列表
                List<Corpus> corpuses = corpusMapper.selectList(wrapper);
                List tempCorpuses = new ArrayList();
                if (corpuses != null && corpuses.size() > 0) {
                    for (Corpus coupus :
                            corpuses) {
                        tempCorpuses.add(coupus.getSolts());
                    }
                }
                resultIntent.put("stencils", tempCorpuses);
                //查询所有的槽位数据信息
                Map paraMap = new HashMap();
                paraMap.put("intentionId", intentionId);
                //List<Map> slotEntitys = slotEntityRefMapper.getSlotEntityList(paraMap);


                /*//每个意图中实体的列表
                List resultEntitys = new ArrayList();
                //每个意图中辅助词列表
                List resultAids = new ArrayList();
                for (Map sloeEntity :
                        slotEntitys) {
                    String type = (String) map.get("type");
                    //槽位
                    String slot = (String) map.get("slot");
                    //对应的实体code
                    String code = map.get("code") == null ? "" : (String) map.get("code");
                    Map resultEntityOrAid;
                    if ("0".equals(type)) {
                        //实体
                        resultEntityOrAid = new HashMap();
                        resultEntityOrAid.put("slot", slot);
                        resultEntityOrAid.put("Matchentity", code);
                        resultEntitys.add(resultEntityOrAid);
                    } else {
                        //辅助词
                        resultEntityOrAid = new HashMap();
                        resultEntityOrAid.put("slot", slot);
                        resultEntityOrAid.put("Matchaid", code);
                        resultAids.add(resultEntityOrAid);
                    }
                }
                resultIntent.put("entitys",resultEntitys);
                resultIntent.put("aid",resultAids);*/
                resultIntents.add(resultIntent);
            }
            resultMap.put("intents", resultIntents);
            //获取每个意图对应的槽位信息
        }
        tempMap.put("skillInfo", resultMap);
        tempMap.put("userName", SecurityUtils.getUsername());
        tempMap.put("text", skill.getText());
        tempMap.put("score", skill.getScore());
        //System.out.println(JSONObject.toJSONString(tempMap));
        return tempMap;
    }
}
